package sound;

import paulscode.sound.SoundSystem;

import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.Renderer;
import com.jme.system.DisplaySystem;

import logic.DelayedController;

public class ListenerPositionController extends DelayedController {
	
	private static final long serialVersionUID = 1L;
	
	private Renderer renderer;
	private SoundSystem system;
	
	public ListenerPositionController(SoundSystem system) {
		super(0.5f);
		this.system = system;
		
		renderer = DisplaySystem.getDisplaySystem().getRenderer();
	}
	
	@Override
	protected void delayedUpdate(float time) {
		Camera cam = renderer.getCamera();
		if(cam != null) {
			Vector3f camLoc = cam.getLocation();
			float x = camLoc.x;
			float y = camLoc.y;
			float z = camLoc.z;
			system.setListenerPosition(x, y, z);
			Vector3f camDir = cam.getDirection().normalize();
			x = camDir.x;
			y = camDir.y;
			z = camDir.z;
			system.setListenerOrientation(x, y, z, Vector3f.UNIT_Y.x, Vector3f.UNIT_Y.y, Vector3f.UNIT_Y.z);
		}
	}
}